//
//  LLSlider.swift
//  LaiLang
//
//  Created by David on 2017/2/3.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

class LLSlider: UIView, UIScrollViewDelegate {

    var scrollView: UIScrollView!
    
    private var _imageViews: [UIView]?
    private var startImageView: UIView?
    private var endImageView: UIView?
    
    private var pageView: UIPageControl!
    
    private var pauseAutoPage = false
    
    var autoPageDuration = 5
    private var autoPageCounting = 0
    
    private var _cycleEnable = true
    var cycleEnable: Bool{
        get{
            return _cycleEnable
        }
        set(v){
            scrollView.alwaysBounceHorizontal = !v
            _cycleEnable = v
        }
    }
    
    var autoPageEnable = true

    var imageViews: [UIView]? {
        get{
            return _imageViews
        }
        set(v){
            if _imageViews != nil {
                for iv in _imageViews!{
                    iv.removeFromSuperview()
                }
            }
            if v != nil {
                scrollView.contentSize = CGSize(width: frame.width * CGFloat(v!.count + 2), height: frame.height)
                var ivs: [UIImageView] = []
                for i in 0..<v!.count{
                    
                    let iv = UIImageView(frame: CGRect(x: frame.width * CGFloat(i+1), y: 0, width: frame.width, height: frame.height))
                    let ivi = v![i] as? UIImageView
                    iv.image = ivi?.image
                    scrollView.addSubview(iv)
                    ivs.append(iv)
                }
                
                startImageView = v![v!.count - 1]
                startImageView!.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
                scrollView.addSubview(startImageView!)
                
                endImageView = v![0]
                endImageView!.frame = CGRect(x: frame.width * CGFloat(v!.count + 1), y: 0, width: frame.width, height: frame.height)
                scrollView.addSubview(endImageView!)
                
                pageView.numberOfPages = v!.count
                
                scrollView.setContentOffset(CGPoint(x: frame.width, y: 0), animated: false)
                
                if ivs.count != 0{
                    _imageViews = ivs
                }else{
                    _imageViews = nil
                }
                
            }
            
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height))
        scrollView.isPagingEnabled = true
//        scrollView.alwaysBounceHorizontal = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        addSubview(scrollView)
        
        pageView = UIPageControl(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
        pageView.center.x = scrollView.center.x
        pageView.center.y = frame.height - 16
        addSubview(pageView)
        
        Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(LLSlider.autoPage), userInfo: nil, repeats: true)
    }
    
    func setImage(at: Int, image: UIImage?) -> Bool {
        if _imageViews != nil {
            if at < _imageViews!.count && at > 0 {
                let imageV = _imageViews![at] as? UIImageView
                if imageV != nil {
                    imageV?.image = image
                    return true
                }
            }
        }
        return false
    }
    
    internal func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if _imageViews != nil {
            if _imageViews!.count != 0 {
                if _cycleEnable {
                    if scrollView.contentOffset.x <= 0{
                        scrollView.setContentOffset(CGPoint(x: frame.width * CGFloat(_imageViews!.count), y: 0), animated: false)
                    }else if scrollView.contentOffset.x >= frame.width * CGFloat(_imageViews!.count + 1){
                        scrollView.setContentOffset(CGPoint(x: frame.width, y: 0), animated: false)
                    }
                }
                
            }
        }
        
        pageView.currentPage = Int(scrollView.contentOffset.x / frame.width) - 1
    }
    
    internal func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        pauseAutoPage = true
    }
    
    internal func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        autoPageCounting = 0
        pauseAutoPage = false
    }
    
    func autoPage(){
        if autoPageEnable{
            if !pauseAutoPage {
                if _imageViews != nil {
                    if _imageViews!.count != 0 {
                        autoPageCounting = (autoPageCounting + 1) % autoPageDuration
                        if autoPageCounting == 0{
                            scrollView.setContentOffset(CGPoint(x: frame.width + CGFloat(pageView.currentPage + 1) * frame.width, y: 0), animated: true)
                        }
                        
                        
                    }
                }
                
            }
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
